package secretaryGUI;

import java.sql.SQLException;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableCellRenderer;

import basic.ClientConsole;

import com.sun.rowset.CachedRowSetImpl;

import flexibility.userData;
import gui.secretaryScreen;
import guiCode.secretaryScreenCode;

public class createNewClass {
	
	secretaryScreenCode ssc;
	secretaryAlerts sa = new secretaryAlerts();
	userData newClass = new userData();
	public void buttonClick(secretaryScreenCode code, createSecretaryWindow csw, ClientConsole secretaryConnection, CachedRowSetImpl crs){
		ssc = code;
		// this will create new class for the semester
		String className = null;

		// this will prompt the user with a message

		className = JOptionPane.showInputDialog(null,
				"Enter new class name", "New class editor",
				JOptionPane.INFORMATION_MESSAGE);

		if (className == null) {
			System.out.println("class = " + className);// case the user
														// presses
														// cancel
		} else {
			if (className.equals("")) {
				// case the user provided empty class name
				sa.showMessages(csw,ssc,csw.secretaryScreen, "You can not create nameless class",
						"Class editor - failure", 1);
			} else {
				if (className != null) {
					int checkResult;
					if ((checkResult=ssc.checkClassRegex(className))==1) {
						// user provided a class
						ssc.createNewClass(csw,ssc,csw.secretaryScreen,className,
								ssc.getSemesterForEdit());
						try {
							// this will refresh the class table
							csw.table.setModel(ssc.getClasses(ssc
									.getSemesterForEdit()));
						} catch (SQLException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						DefaultTableCellRenderer centerRenderer = new DefaultTableCellRenderer();
						centerRenderer
								.setHorizontalAlignment(JLabel.CENTER);
						for (int i = 0; i < csw.table.getColumnCount(); i++)
							csw.table.getColumnModel().getColumn(i)
									.setCellRenderer(centerRenderer);
					} else {
						if(checkResult==0){
							sa.showMessages(
									csw,ssc,csw.secretaryScreen, "Class name must be of type [1-12][A-Z]\nFor example: 1A, 9C, ...",
									"Class editor - failure", 1);
						}
						if(checkResult==2){
							
						}
						
					}
				}

			}

		}
	
	}
}
